package com.ruoyi.web.controller.building;


import com.ruoyi.building.domain.AmBuilding;
import com.ruoyi.building.service.IAmBuildingService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.AmProject;
import com.ruoyi.project.service.IAmProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;


/**
 * 楼宇管理Controller
 *
 * @author ruoyi
 * @date 2020-07-28
 */

@RestController
@RequestMapping("/building/building")
@Api(description = "楼宇管理")
public class AmBuildingController extends BaseController
{
	@Autowired
	private IAmBuildingService amBuildingService;

	@Autowired
	private IAmProjectService projectService;

	/**
	 * 查询楼宇管理列表
	 */

	@PreAuthorize("@ss.hasPermi('building:building:list')")
	@GetMapping("/list")
	@ApiOperation("查询楼宇管理列表")
	public TableDataInfo list(AmBuilding amBuilding)
	{
		startPage();
		List<AmBuilding> list = amBuildingService.selectAmBuildingList(amBuilding);
		return getDataTable(list);
	}

	@GetMapping("/listByProject")
	@ApiOperation("根据项目查询楼宇列表")
	public AjaxResult listByProject(Long id)
	{
		List<HashMap<String,Object>> list = amBuildingService.listByProject(id);
		return AjaxResult.success(list);
	}


	/**
	 * 导出楼宇管理列表
	 */

	@PreAuthorize("@ss.hasPermi('building:building:export')")
	@Log(title = "楼宇管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	@ApiOperation("导出楼宇管理列表")
	public AjaxResult export(AmBuilding amBuilding)
	{
		List<AmBuilding> list = amBuildingService.selectAmBuildingList(amBuilding);
		ExcelUtil<AmBuilding> util = new ExcelUtil<AmBuilding>(AmBuilding.class);
		return util.exportExcel(list, "building");
	}

	@GetMapping("/importTemplate")
	@ApiOperation("导入模板")
	public AjaxResult importTemplate()
	{
		ExcelUtil<AmBuilding> util = new ExcelUtil<AmBuilding>(AmBuilding.class);
		return util.importTemplateExcel("楼宇数据");
	}


	@Log(title = "楼宇管理", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasPermi('building:building:import')")
	@PostMapping("/importData")
	@ApiOperation("导入接口")
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
	{
		if(null == file || !file.getOriginalFilename().contains("xls")){
			return AjaxResult.error("格式不支持");
		}

		List<String> projectList = projectService.selectAllAmProjectList();
		List<String> buildList = amBuildingService.selectAllAmBuildingList();

		ExcelUtil<AmBuilding> util = new ExcelUtil<AmBuilding>(AmBuilding.class);
		List<AmBuilding> buildings = util.importExcel(file.getInputStream());
		if (null == buildings || buildings.size() == 0 ){
			return AjaxResult.error("请不要上传空文件");
		}
		for (int i = 0 ; i<buildings.size() ; i++){
			AmBuilding building = buildings.get(i);
			if (StringUtils.isNull(building)) {
				return AjaxResult.error("第"+(i+2)+"行数据缺失或者表格不正确,请检查后在重新导入");
			}else if(StringUtils.isEmpty(building.getBuildingName() )){
				return AjaxResult.error("第"+(i+2)+"行楼宇名称缺失,请检查后在重新导入");
			}else if(StringUtils.isNull(building.getFloorStart())){
				return AjaxResult.error("第"+(i+2)+"行开始楼层缺失,请检查后在重新导入");
			}else if( StringUtils.isNull(building.getFloorEnd() )){
				return AjaxResult.error("第"+(i+2)+"行结束楼层缺失,请检查后在重新导入");
			}else if( StringUtils.isEmpty( building.getProjectName())){
				return AjaxResult.error("第"+(i+2)+"行项目名称缺失,请检查后在重新导入");
			}else if (!projectList.contains(building.getProjectName())){
				return AjaxResult.error("第"+(i+2)+"行项目名称未找到,请检查后在重新导入");
			}else if (buildList.contains(building.getProjectName()+building.getBuildingName())){
				return AjaxResult.error("第"+(i+2)+"行楼宇名称重复,请修改后在重新导入");
			}else if(0 == building.getFloorStart()){
				return AjaxResult.error("第"+(i+2)+"行开始楼层不能为0,请修改后在重新导入");
			}else if(0 == building.getFloorEnd()){
				return AjaxResult.error("第"+(i+2)+"行结束楼层不能为0,请修改后在重新导入");
			}

		}
		String message = amBuildingService.importProject(buildings, updateSupport);
		return AjaxResult.success(message);
	}
	/**
	 * 获取楼宇管理详细信息
	 */

	@PreAuthorize("@ss.hasPermi('building:building:query')")
	@GetMapping(value = "/{id}")
	@ApiOperation("获取楼宇管理详细信息")
	public AjaxResult getInfo(@PathVariable("id") Long id)
	{
		return AjaxResult.success(amBuildingService.selectAmBuildingById(id));
	}


	/**
	 * 新增楼宇管理
	 */

	@PreAuthorize("@ss.hasPermi('building:building:add')")
	@Log(title = "楼宇管理", businessType = BusinessType.INSERT)
	@PostMapping
	@ApiOperation("新增楼宇管理")
	public AjaxResult add(@RequestBody AmBuilding amBuilding)
	{
		if (null == amBuilding.getFloorStart()){
			return AjaxResult.error("开始楼层不能为空");
		}
		if ( amBuilding.getFloorStart().equals(0L)){
			return AjaxResult.error("开始楼层不能为0");
		}
		if (null == amBuilding.getFloorEnd() ){
			return AjaxResult.error("结束楼层不能为空");
		}
		if ( amBuilding.getFloorEnd().equals(0L)){
			return AjaxResult.error("结束楼层不能为0");
		}
		if (amBuilding.getFloorEnd() < amBuilding.getFloorStart()){
			return AjaxResult.error("结束楼层不能小于开始楼层");
		}
		return toAjax(amBuildingService.insertAmBuilding(amBuilding));
	}


	/**
	 * 修改楼宇管理
	 */

	@PreAuthorize("@ss.hasPermi('building:building:edit')")
	@Log(title = "楼宇管理", businessType = BusinessType.UPDATE)
	@PutMapping
	@ApiOperation("修改楼宇管理")
	public AjaxResult edit(@RequestBody AmBuilding amBuilding)
	{
		if (null == amBuilding.getFloorStart() || amBuilding.getFloorStart().equals(0L)){
			return AjaxResult.error("开始楼层不能为0");
		}
		if (null == amBuilding.getFloorEnd() || amBuilding.getFloorEnd().equals(0L)){
			return AjaxResult.error("结束楼层不能为0");
		}
		return toAjax(amBuildingService.updateAmBuilding(amBuilding));
	}


	/**
	 * 删除楼宇管理
	 */
	@PreAuthorize("@ss.hasPermi('building:building:remove')")
	@Log(title = "楼宇管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	@ApiOperation("删除楼宇管理")
	public AjaxResult remove(@PathVariable Long[] ids)
	{
		return toAjax(amBuildingService.deleteAmBuildingByIds(ids));
	}


	/**
	 * 获取楼宇楼层
	 */
	@GetMapping("/getFloorList/{buildingId}")
	public AjaxResult getFloorList(@PathVariable Long buildingId){
		return AjaxResult.success(amBuildingService.getFloorList(buildingId));
	}
}

